package com.wlts.wzy.SelfModule.mamagerController;


import com.alibaba.fastjson.JSON;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.wlts.wzy.Base.BaseEntity;
import com.wlts.wzy.Base.BaseIdParam;
import com.wlts.wzy.Base.BasePageParam;
import com.wlts.wzy.Base.IBaseController;
import com.wlts.wzy.Result.Result;
import com.wlts.wzy.SelfModule.entity.SelfGoods;
import com.wlts.wzy.SelfModule.service.ISelfGoodsService;
import com.wlts.wzy.api.request.self.AddSelfGoodsRequest;
import com.wlts.wzy.api.request.self.PageSelfGoodsRequest;
import com.wlts.wzy.expand.MyLambdaQueryWrapper;
import com.wlts.wzy.expand.MyPage;
import com.wlts.wzy.util.CopyUtils;
import com.wlts.wzy.util.StringUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 自营商品表 前端控制器
 * </p>
 *
 * @author zhiyong
 * @since 2021-03-19
 */
@Api(tags = "自营商品表 后台管理控制器",position = 10)
@RestController
@RequestMapping("/SelfModule/self-goods")
@ApiSort(value = 10)
public class SelfGoodsManagerController {

    @Autowired
    private ISelfGoodsService selfGoodsService;

    @RequestMapping(value = "/addEntity",method = RequestMethod.POST)
    public Result<SelfGoods> addEntity(@RequestBody AddSelfGoodsRequest baseEntity) {

        SelfGoods selfGoods=new SelfGoods();

        CopyUtils.copyProperties(baseEntity,selfGoods);

        baseEntity.getSkuList().forEach(selfGoodsSkuVO -> {
            String skuId=System.currentTimeMillis()+ StringUtils.getRandomIntByLength(2);
            selfGoodsSkuVO.setSkuId(Long.valueOf(skuId));
        });
        String ItemId=System.currentTimeMillis()+ StringUtils.getRandomIntByLength(2);
        selfGoods.setItemId(Long.valueOf(ItemId));
        selfGoods.setSkuList(JSON.toJSONString(baseEntity.getSkuList()));
        selfGoods.setMainPicPrl(JSON.toJSONString(baseEntity.getMainPicPrl()));
        selfGoods.setPicList(JSON.toJSONString(baseEntity.getPicList()));
        selfGoodsService.save(selfGoods);
        return Result.buildResult(selfGoods);
    }

    @RequestMapping(value = "/delById",method = RequestMethod.POST)
    public Result<Boolean> delById(@RequestBody BaseIdParam t) {
        return Result.buildResult(selfGoodsService.removeById(t.getId()));
    }

    @RequestMapping(value = "/chaEntity",method = RequestMethod.POST)
    public Result chaEntity(@RequestBody AddSelfGoodsRequest baseEntity) {
        SelfGoods selfGoods=selfGoodsService.getById(baseEntity.getId());
        CopyUtils.copyProperties(baseEntity,selfGoods);
        selfGoods.setSkuList(JSON.toJSONString(baseEntity.getSkuList()));
        selfGoods.setMainPicPrl(JSON.toJSONString(baseEntity.getMainPicPrl()));
        selfGoods.setPicList(JSON.toJSONString(baseEntity.getPicList()));
        selfGoodsService.updateById(selfGoods);
        return Result.buildResult(selfGoods);
    }

    @RequestMapping(value = "/getById",method = RequestMethod.POST)
    public Result getById(@RequestBody BaseIdParam t) {
        return Result.buildResult(selfGoodsService.getById(t.getId()));
    }

    @RequestMapping(value = "/getPage",method = RequestMethod.POST)
    public Result<MyPage<SelfGoods>> getPage(@RequestBody PageSelfGoodsRequest request) {

        SelfGoods selfGoods=new SelfGoods();
        CopyUtils.copyProperties(request,selfGoods);

        selfGoods.setItemId(null);

        MyLambdaQueryWrapper<SelfGoods> wrapper = new MyLambdaQueryWrapper<>(selfGoods);

        if(StringUtils.isNotEmpty(request.getItemTitle())){
            wrapper.like(SelfGoods::getItemTitle,request.getItemTitle());
        }
        wrapper.eq(BaseEntity::getDelFlag, "0");

        MyPage<SelfGoods> brandMyPage = selfGoodsService.page(wrapper, request.getCurrentPage(), request.getPageSize(), true);

        return Result.buildResult(brandMyPage);
    }

}
